﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SharePoint.UserCode;

namespace Com.CodePlex.SPC3.DatabaseProxy {

    public class SqlCommandProxyOperation : SPProxyOperation {

        public override object Execute(SPProxyOperationArgs a) {
            try {
                DatabaseProxyArgs args = a as DatabaseProxyArgs;
                if (args != null) {
                    using (SqlConnection conn = new SqlConnection(args.ConnectionString)) {
                        conn.Open();
                        using (SqlCommand cmd = new SqlCommand(args.Command, conn)) {
                            cmd.CommandType = args.CommandType;
                            foreach (KeyValuePair<string, object> para in args.Parameters) {
                                cmd.Parameters.AddWithValue(para.Key, para.Value);
                            }
                            return cmd.ExecuteNonQuery();
                        }
                    }
                }
            } catch (Exception ex) {
                return ex.ToString();
            }
            return null;
        }
    }
}
